package com.woniu.controller;

import com.woniu.entity.RechargeRecord;
import com.woniu.service.RechargeRecordService;
import com.woniu.util.ResponseData;
import com.woniu.util.ResponseEnum;
import com.woniu.util.easyExcelUtil.EasyExcelUtil;
import com.woniu.vo.RechargeRecordVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**
 * FileName: RechargeRecordController
 * Author: wujiajun
 * Date:   2025/2/19 12:08
 * Description:DOTO
 */
@RestController
@RequestMapping("/recharge")
public class RechargeRecordController {
    @Resource
    private RechargeRecordService rechargeRecordService;

    @GetMapping("/list")
    public ResponseData<?> getRechargeRecordList(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        List<RechargeRecordVO> records = rechargeRecordService.getRechargeRecordList(keyword, pageNum, pageSize);
        return ResponseData.ok(records);
    }

    @PostMapping("/add")
    public ResponseData<?> addRechargeRecord(@RequestBody RechargeRecord rechargeRecord) {
        boolean success = rechargeRecordService.addRechargeRecord(rechargeRecord);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @DeleteMapping("/{id}")
    public ResponseData<?> deleteRechargeRecord(@PathVariable("id") Long id) {
        boolean success = rechargeRecordService.deleteRechargeRecord(id);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @PostMapping("/export")
    public void exportRechargeRecords(
            HttpServletResponse response,
            @RequestParam(required = false) String keyword
    ) throws IOException {
        // 设置响应头，防止中文乱码
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        
        // 获取所有充值记录数据
        List<RechargeRecordVO> records = rechargeRecordService.getRechargeRecordList(keyword, 1, Integer.MAX_VALUE);
        
        // 设置表头
        List<List<String>> headList = new ArrayList<>();
        headList.add(Collections.singletonList("订单号"));
        headList.add(Collections.singletonList("客户姓名"));
        headList.add(Collections.singletonList("联系电话"));
        headList.add(Collections.singletonList("支付方式"));
        headList.add(Collections.singletonList("充值金额(元)"));

        // 转换数据格式
        List<List<Object>> dataList = new ArrayList<>();
        
        for (RechargeRecordVO record : records) {
            List<Object> rowData = new ArrayList<>();
            rowData.add(record.getOrderNumber());
            rowData.add(record.getCustomerName());
            rowData.add(record.getPhoneNumber());
            rowData.add(record.getPaymentMethod());
            rowData.add(record.getAmount());
            dataList.add(rowData);
        }

        // 使用EasyExcel导出
        String fileName = "充值记录_" + System.currentTimeMillis();
        try {
            EasyExcelUtil.writeWithSheetsWeb(response, fileName)
                    .write(headList, dataList, "充值记录")
                    .finish();
        } catch (Exception e) {
            e.printStackTrace();
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println("下载文件失败：" + e.getMessage());
        }
    }
}
